package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.controller.dto.NursingPlanDto;
import com.zzyl.nursing.controller.dto.NursingProjectPlanDto;
import com.zzyl.nursing.controller.vo.NursingPlanVo;
import com.zzyl.nursing.controller.vo.NursingPlansVo;
import com.zzyl.nursing.controller.vo.NursingProjectPlanVo;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-15
 */
@Service
public class NursingPlanServiceImpl implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    @Transactional
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //查护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();

        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);

        //根据护理计划ID查询护理项目的关系
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectNursingProjectPlanByPlanId(id);

        nursingPlanVo.setProjectPlans(list);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingProjectPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingProjectPlanDto nursingProjectPlanDto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingProjectPlanDto,nursingPlan);
        nursingPlan.setCreateTime(new Date());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        List<NursingProjectPlan> projectPlans = nursingProjectPlanDto.getProjectPlans();
        //判断是否为空
        if (!Collections.isEmpty(projectPlans)){
            /*for (NursingProjectPlan projectPlan : projectPlans) {
                projectPlan.setPlanId(nursingPlan.getId());*/
            //向关系表中批量插入数据
            int num =  nursingProjectPlanMapper.insertBatch(projectPlans, nursingPlan.getId());
            return num > 0 ? 1 : 0;
        }
        return 0;
    }


/**
 * 修改护理计划
 *
 * @param nursingPlanDto 护理计划
 * @return 结果
 */
@Override
@Transactional
public int updateNursingPlan(NursingPlanDto nursingPlanDto)
{
    NursingPlan nursingPlan = new NursingPlan();
    BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
    int i = nursingPlanMapper.updateNursingPlan(nursingPlan);

    List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
    //先删除再插入
    if (!Collections.isEmpty(projectPlans)){
        //删除
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(nursingPlan.getId());
        //插入
        nursingProjectPlanMapper.insertBatch(projectPlans, nursingPlan.getId());
    }
    return i;
}

/**
 * 批量删除护理计划
 *
 * @param ids 需要删除的护理计划主键
 * @return 结果
 */
@Override
public int deleteNursingPlanByIds(Long[] ids)
{
    return nursingPlanMapper.deleteNursingPlanByIds(ids);
}

/**
 * 删除护理计划信息
 *
 * @param id 护理计划主键
 * @return 结果
 */
@Override
public int deleteNursingPlanById(Long id)
{
    return nursingPlanMapper.deleteNursingPlanById(id);
}

    /**
     * 查询所有计划
     * @return
     */
    @Override
    public List<NursingPlansVo> getAllPlans() {
        List<NursingPlansVo> list = nursingPlanMapper.getAllPlans();
        return list;
    }
}

